package dao;

import hbt.HbtUtil;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import bean.ClienteVO;
import bean.OficinaDeVentaVO;
import bean.RodamientoVO;

public class HbtClienteDao {
	private static HbtClienteDao instancia = null;
	private static SessionFactory sf = null;

	public static HbtClienteDao getInstancia() {
		if (instancia == null) {
			sf = HbtUtil.getSessionFactory();
			instancia = new HbtClienteDao();
		}
		return instancia;
	}

	public void grabarClientes(List<ClienteVO> clientes) {
		Session session = sf.openSession();
		session.beginTransaction();
		session.persist(clientes);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	public void guardarCliente(ClienteVO cliente) {
		Session session = sf.openSession();
		session.beginTransaction();
		session.persist(cliente);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	public void eliminarCliente(ClienteVO c) {

		String cuit = c.getCuit();
		System.out.println(cuit);
		System.out.println(cuit.length());

		Session s = null;
		System.out.println("ejecuta eliminar ");
		try {
			s = sf.openSession();
			s.beginTransaction();
			Query query = s.createQuery(
					"Delete from ClienteVO c where c.cuit = :cuit").setString(
					"cuit", cuit);
			query.executeUpdate();
			System.out.println("entra try");
			s.flush();
			s.getTransaction().commit();
			s.close();

		} catch (HibernateException e) {
			e.printStackTrace();
			System.out.println("No  se puedo borrar el cliente");
			if (s != null)
				s.close();
		}

	}

	public ClienteVO obtenerCliente(ClienteVO c) {
		Session session = sf.openSession();
		ClienteVO cliente = (ClienteVO) session.get(ClienteVO.class,
				c.getCuit());
		session.close();
		return cliente;
	}
	
	public RodamientoVO obtenerRodamiento(RodamientoVO r) {
		Session session = sf.openSession();
		RodamientoVO rodamiento = (RodamientoVO)session.get(RodamientoVO.class, r.getIdRodamiento());
		session.close();
		return rodamiento;
	}

	public void actualizarCliente(ClienteVO cliente) {
		try {

			Session session = sf.openSession();
			session.beginTransaction();
			session.update(cliente);
			session.getTransaction().commit();
			session.close();

		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
	
	

	@SuppressWarnings("unchecked")
	public List<ClienteVO> obtenerTodos(OficinaDeVentaVO o) {
		List<ClienteVO> clientes = new ArrayList<ClienteVO>();
		int nro = o.getNroOficina();
		Session session = null;
		session = sf.openSession();
		session.beginTransaction();
		clientes = session
				.createQuery(
						"from ClienteVO c where c.oficina.nroOficina = :nro")
				.setInteger("nro", nro).list();
		session.getTransaction().commit();
		session.close();
		return clientes;

	}


}
